﻿package mortal.game.view.copy.defenseCopy
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.groupCopy.data.*;
    import mortal.game.view.copy.groupCopy.view.*;
    import mortal.game.view.guildNew.*;
    import mortal.game.view.mainUI.smallMap.view.*;
    import mortal.game.view.signUp.sevenDayLogin.item.*;
    import mortal.mvc.core.*;

    public class DefenseMiniMap extends CopyInfoPanelBase
    {
        private var _bgSprite:Sprite;
        private var _moveInfoSp:Sprite;
        private var _bgImg:GBitmap;
        private var _leftTime:SecTimerView;
        private var _lineBg:ScaleBitmap;
        private var _smallMap:SmallMapEmbedView;
        private var _txtDefenseWave:GTextFiled;
        private var _coin:GTextFiled;
        protected var _rentBtn:GLoadingButton;
        private var _txtShop:GTextFiled;
        private var _txtBlood:GTextFiled;
        private var _curLv:GTextFiled;
        private var _avatar:GImageBitmap;
        private var _bossName:GTextFiled;
        private var _bigLv:GTextFiled;
        private var _itemReward:GTileList;
        protected var _qmList:CellsGridSprite;
        private var _maskList:HideAreaSp;
        protected var _expDes:GTextFiled;
        private var _HPProgressBar:BaseProgressBar;
        private var _bossHasEvent:Boolean = false;
        private var _bossHasDie:Boolean = false;
        private var _bossEntity:EntityInfo;
        private var _uiInitOver:Boolean = false;
        private var _curWave:int = -1;
        private var bossStuffCode:int;
        protected var _bossCk:GButton;
        private var _isLoad:Boolean = false;
        private var firstFlag:Boolean = true;
        private var showTime:int;
        private var bossCode:int;
        private var _maxWave:int;
        public static var slabelName:String;
        private static const TTout:Number = 0.5;

        public function DefenseMiniMap()
        {
            super("", 250, 520);
            mouseEnabled = false;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            if (slabelName != "")
            {
                _labelName = slabelName;
            }
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.defenseCopy, this.showSkin);
            this.stageResize();
            return;
        }// end function

        private function initLife() : void
        {
            var _loc_2:SAttributeUpdate = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_1:* = Cache.instance.copy.defenseCopy.szBlood;
            if (_loc_1)
            {
                _loc_2 = _loc_1.updates[0];
                _loc_3 = _loc_2.value;
                _loc_4 = parseInt(_loc_2.valueStr);
                this.updateLife(_loc_3, _loc_4);
            }
            return;
        }// end function

        private function showSkin() : void
        {
            if (isDisposed)
            {
                return;
            }
            this._isLoad = true;
            _moveBtn.move(-5, -10);
            _btnUnExpand.move(217, -11);
            this._bgSprite = UIFactory.sprite(0, 0, _bodySprite);
            this._bgSprite.mouseEnabled = false;
            this._bgSprite.mouseChildren = false;
            this._moveInfoSp = UIFactory.sprite(0, 0, _bodySprite);
            this._moveInfoSp.mouseEnabled = false;
            this._moveInfoSp.mouseChildren = true;
            this._bgImg = UIFactory.gBitmap(ImagesConst.DefenseMiniMapBg, -11, -22, this._bgSprite);
            this._curLv = UIFactory.textField("" + Language.getString(70870), 17, -9, 198, 20, _bodySprite, GlobalStyle.textFormatHuang2);
            this._leftTime = UIFactory.secTimeView("" + Language.getString(70872), 88, -9, 120, 22, _bodySprite, GlobalStyle.textFormatHuang2.right());
            this._leftTime.timeOutHandler = this.timeCompl;
            this._smallMap = UIFactory.smallMapEmbedView(this.getSmallBgUrl(), 0, 20, _bodySprite, true);
            this._smallMap.run();
            this._lineBg = UIFactory.bg(0, 20, 228, 24, _bodySprite, ImagesConst.RegionTitleBg);
            this._txtDefenseWave = UIFactory.textField("" + Language.getString(70873), 0, 22, 230, 22, _bodySprite, GlobalStyle.textFormatChen.center());
            pushUIToDisposeVec(UIFactory.textField("" + Language.getString(70874), 163, 228, 80, 22, _bodySprite, GlobalStyle.textFormatHuang2));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.DefenseCopyTxt2, 11, 267, _bodySprite));
            this._coin = UIFactory.gTextField("", 77, 474, 100, 20, this._moveInfoSp, GlobalStyle.textFormatHuang);
            this._rentBtn = UIFactory.gLoadingButton(ImagesConst.GuildNewMercenaryRentBtn_upSkin, 137, 428, 90, 45, _bodySprite);
            this._rentBtn.label = "";
            this._rentBtn.configEventListener(MouseEvent.CLICK, this.onRentCk);
            this._rentBtn.visible = false;
            this._txtShop = UIFactory.gTextField("", 172, 475, 67, 22, this._moveInfoSp);
            this._txtShop.htmlText = HTMLUtil.addColor("<u><a href=\'event:1\'>" + Language.getString(70870) + "</a></u>", GlobalStyle.colorLv);
            this._txtShop.configEventListener(TextEvent.LINK, this.onTextLink);
            this._txtBlood = UIFactory.gTextField("", 3, 228, 67, 22, _bodySprite);
            this._txtBlood.htmlText = HTMLUtil.addColor("" + Language.getString(70875), GlobalStyle.colorHuang2);
            var _loc_1:* = BossConfig.instance.getInfoByCode(GameConst.DefenseCopyGoldStuffCode);
            this._HPProgressBar = UIFactory.getUICompoment(BaseProgressBar, 56, 231, _bodySprite);
            this._HPProgressBar.setBg(ImagesConst.PetLifeBg, true, 107, 15);
            this._HPProgressBar.setProgress(ImagesConst.GuildWarSelfProgress, true, 2, 2, 104, 12);
            this._HPProgressBar.setLabel(BaseProgressBar.ProgressBarTextNumber, -43, -3);
            this._HPProgressBar.mouseChildren = false;
            this._HPProgressBar.mouseEnabled = true;
            this._HPProgressBar.setValue(0, _loc_1.maxLife);
            this._avatar = UIFactory.gImageBitmap("", 84, 290, _bodySprite);
            this._bossName = UIFactory.gTextField("", 146, 263, 67, 22, _bodySprite, GlobalStyle.textFormatHuang2.center());
            this._bigLv = UIFactory.gTextField("100", 36, 263, 100, 20, _bodySprite, GlobalStyle.textFormatHuang2.center());
            this._itemReward = UIFactory.tileList(40, 376, 280, 50, _bodySprite);
            this._itemReward.setStyle("cellRenderer", ItemCellRenderer);
            this._itemReward.columnWidth = 44;
            this._itemReward.rowHeight = 45;
            this._itemReward.selectable = false;
            this._itemReward.direction = GBoxDirection.HORIZONTAL;
            this._qmList = UIFactory.getUICompoment(CellsGridSprite);
            this._qmList.colNum = 100;
            this._qmList.vCellWid = 43;
            this._qmList.hCellWid = 100;
            this._qmList.genCellFun = UIFactory.getUICompoment;
            this._qmList.genCells(4, DefenseMiniQmCell);
            this._qmList.x = 41;
            this._qmList.y = 376;
            this._bossCk = UIFactory.gButton("", 76, 283, 76, 80, _bodySprite);
            this._bossCk.alpha = 0.01;
            this._bossCk.configEventListener(MouseEvent.CLICK, this.onBossSelect);
            this._maskList = UIFactory.getUICompoment(HideAreaSp, this._qmList.x, this._qmList.y);
            this._maskList.drawRec(this._qmList.width, this._qmList.height, 0.01);
            this._expDes = UIFactory.gTextField(Language.getString(70876) + "0", 0, 431, 150, 25, _bodySprite, GlobalStyle.textFormatHuang2.setSize(15).setBold(true));
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsAdd, this.onPackItemsAdd);
            this._uiInitOver = true;
            this.setBossStuffCode();
            this.updateAll();
            return;
        }// end function

        private function onCk(event:MouseEvent) : void
        {
            this.toFly();
            return;
        }// end function

        private function onClickHide(event:MouseEvent) : void
        {
            this._bodySprite.visible = !this._bodySprite.visible;
            return;
        }// end function

        public function updateAll(param1:Object = null) : void
        {
            if (!this._uiInitOver)
            {
                return;
            }
            var _loc_2:* = Cache.instance.copy.defenseCopy;
            if (_loc_2.copyInfo)
            {
                if (_loc_2.copyInfo.surplusSeconds > 0 && this._leftTime.secTimerViewData == null || _loc_2.isCopyEnd)
                {
                    this._leftTime.setLeftTime(_loc_2.copyInfo.surplusSeconds);
                }
                this._curLv.text = Language.getStringByParam(70877, _loc_2.copyInfo.mainFightIndex);
                this._txtDefenseWave.text = Language.getStringByParam(70878, _loc_2.copyInfo.mainBoss);
                _loc_2.boNum = _loc_2.copyInfo.mainFightIndex;
                _loc_2.totalBoNum = _loc_2.copyInfo.totalIndex;
            }
            this.updateRewards();
            if (_loc_2.copyReward)
            {
                this._coin.text = _loc_2.copyReward.equipPoint.toString();
            }
            if (_loc_2.dropItemMsg)
            {
                this._expDes.text = Language.getString(70879) + _loc_2.dropItemMsg.exp;
                if (_loc_2.dropItemMsg.exp > 0)
                {
                    this._expDes.visible = true;
                }
                else
                {
                    this._expDes.visible = false;
                }
            }
            this.initLife();
            return;
        }// end function

        private function onBossSelect(event:MouseEvent) : void
        {
            var _loc_2:* = Cache.instance.copy.defenseCopy;
            if (_loc_2)
            {
                this.bossCode = _loc_2.dropItemMsg.bossCode;
                ThingUtil.entityUtil.entitysMap.selectEntityByMonsterCode(this.bossCode);
            }
            return;
        }// end function

        private function timeDelayUpdate() : void
        {
            clearTimeout(this.showTime);
            this.firstFlag = true;
            this.toUpdateRews();
            this._qmList.visible = true;
            this._maskList.visible = this._qmList.visible;
            return;
        }// end function

        private function updateRewards() : void
        {
            var _loc_1:* = Cache.instance.copy.defenseCopy;
            if (_loc_1.dropItemMsg && _loc_1.dropItemMsg.wave != this._curWave)
            {
                if (this._curWave == -1)
                {
                    this.toUpdateRews();
                    this._qmList.visible = true;
                    this._maskList.visible = this._qmList.visible;
                }
                else if (this.maxWave == _loc_1.dropItemMsg.wave)
                {
                    this.toUpdateRews();
                    this._qmList.visible = false;
                    this._maskList.visible = this._qmList.visible;
                }
                else if (this.firstFlag)
                {
                    this.firstFlag = false;
                    this._qmList.visible = false;
                    this._maskList.visible = this._qmList.visible;
                    this.toFly();
                    this.showTime = setTimeout(this.timeDelayUpdate, int(TTout * 1000));
                }
            }
            return;
        }// end function

        private function toUpdateRews() : void
        {
            var _loc_4:ItemData = null;
            var _loc_5:String = null;
            var _loc_1:* = Cache.instance.copy.defenseCopy;
            this._curWave = _loc_1.dropItemMsg.wave;
            this.bossCode = _loc_1.dropItemMsg.bossCode;
            var _loc_2:* = BossConfig.instance.getInfoByCode(this.bossCode);
            this._avatar.imgUrl = _loc_2.avatarId + ".png";
            this._bossName.text = _loc_2.name;
            this._bigLv.text = _loc_1.dropItemMsg.wave.toString();
            var _loc_3:Array = [];
            for (_loc_5 in _loc_1.dropItemMsg.items)
            {
                
                _loc_4 = new ItemData(int(_loc_5));
                _loc_4.itemAmount = _loc_1.dropItemMsg.items[_loc_5] as int;
                _loc_3.push(_loc_4);
            }
            if (_loc_3.length > 4)
            {
                _loc_3.length = 4;
            }
            this._itemReward.filters = null;
            this._itemReward.dataProvider = new DataProvider(_loc_3);
            this._itemReward.drawNow();
            return;
        }// end function

        private function onPackItemsAdd(param1:Object) : void
        {
            if (param1[1] == UpdateCode.EUpdateCodeDefenseDropAdd)
            {
                this.toFly();
            }
            return;
        }// end function

        private function toFly() : void
        {
            var _loc_2:ItemData = null;
            var _loc_3:ItemCellRenderer = null;
            var _loc_1:* = this._itemReward.dataProvider.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_1)
            {
                
                _loc_2 = this._itemReward.dataProvider.getItemAt(_loc_4) as ItemData;
                _loc_3 = this._itemReward.itemToCellRenderer(_loc_2) as ItemCellRenderer;
                if (_loc_3 && _loc_3.itemBitmapData && !ItemsUtil.isDefenseEquip(_loc_2))
                {
                    this.toFlyBag(_loc_3.itemBitmapData, _loc_3.localToGlobal(new Point(_loc_3.x, _loc_3.y)));
                }
                _loc_4++;
            }
            return;
        }// end function

        private function toFlyBag(param1:BitmapData, param2:Point, param3:String = "pack") : void
        {
            if (!DisplayUtil.isBitmapDataUsable(param1) || param2 == null)
            {
                return;
            }
            var _loc_4:* = new Point(1001, 720);
            var _loc_5:* = param1.width;
            var _loc_6:* = param1.height;
            var _loc_7:* = 20 / Math.max(_loc_5, _loc_6);
            FlyToNavbarTool.flyToAnyPoint(param1, param2, _loc_4, null, _loc_5, _loc_6, false, _loc_7, _loc_7, true, param3);
            return;
        }// end function

        public function updateLife(param1:int, param2:int) : void
        {
            if (this._HPProgressBar)
            {
                this._HPProgressBar.setValue(param1, param2);
            }
            return;
        }// end function

        private function onRentCk(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildNewMercenaryOpen, GuildNewModule.Mercenary));
            return;
        }// end function

        private function onTextLink(event:TextEvent) : void
        {
            LayerManager.windowLayer.tweenCenterWindow(DefenseCopyShopNew.instance, DefenseCopyBag.instance);
            return;
        }// end function

        private function timeCompl() : void
        {
            this._leftTime.stop();
            return;
        }// end function

        private function timeCompl2() : void
        {
            this._moveInfoSp.y = 0;
            this._lineBg.height = 24;
            return;
        }// end function

        override public function stageResize() : void
        {
            super.stageResize();
            x = Global.stage.stageWidth - 30 - 238;
            y = 172;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsAdd, this.onPackItemsAdd);
            this._curWave = -1;
            this.bossCode = -1;
            this._uiInitOver = false;
            this._bossHasEvent = false;
            this._bossHasDie = false;
            this._bossEntity = null;
            this._maxWave = 0;
            if (this.showTime > 0)
            {
                clearTimeout(this.showTime);
            }
            LoaderHelp.removeResEvent(ResFileConst.defenseCopy, this.showSkin);
            if (this._isLoad == false)
            {
                return;
            }
            this._isLoad = false;
            this._rentBtn = null;
            this._bossCk = null;
            this.firstFlag = true;
            this._bgImg.dispose(param1);
            this._curLv.dispose(param1);
            this._leftTime.dispose(param1);
            this._lineBg.dispose(param1);
            this._txtDefenseWave.dispose(param1);
            this._coin.dispose(param1);
            this._txtShop.dispose(param1);
            this._txtBlood.dispose(param1);
            this._HPProgressBar.dispose(param1);
            this._smallMap.dispose(param1);
            this._avatar.dispose(param1);
            this._bossName.dispose(param1);
            this._bigLv.dispose(param1);
            this._itemReward.dispose(param1);
            this._itemReward = null;
            this._bigLv = null;
            this._bossName = null;
            this._avatar = null;
            this._bgImg = null;
            this._leftTime = null;
            this._txtDefenseWave = null;
            this._lineBg = null;
            this._coin = null;
            this._txtShop = null;
            this._txtBlood = null;
            this._HPProgressBar = null;
            this._smallMap = null;
            this._curLv = null;
            this._expDes = null;
            this._qmList = null;
            this._maskList = null;
            UIFactory.disposeSprite(this._bgSprite);
            this._bgSprite = null;
            UIFactory.disposeSprite(this._moveInfoSp);
            this._moveInfoSp = null;
            return;
        }// end function

        private function getSmallBgUrl() : String
        {
            var _loc_1:* = GroupCopyUtil.getSmallMapId(Cache.instance.copy.copyInfo.code);
            var _loc_2:* = "defenseCopyMiniMap_" + _loc_1 + ".jpg";
            return _loc_2;
        }// end function

        private function setBossStuffCode() : void
        {
            this.bossStuffCode = GameConst.DefenseCopyGoldStuffCode;
            if (GroupCopyGroupDefensePanel.GroupDefenCopyId == Cache.instance.copy.copyInfo.code)
            {
                this.bossStuffCode = GameConst.TConstTeamDefenseCopyGoldStuffCode;
            }
            return;
        }// end function

        private function get maxWave() : int
        {
            var _loc_1:int = 0;
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:TBossRefresh = null;
            if (this._maxWave == 0)
            {
                _loc_1 = Game.mapInfo.mapId;
                _loc_2 = BossConfig.instance.getRefreshByMapId(_loc_1, false);
                _loc_3 = 0;
                while (_loc_3 < _loc_2.length)
                {
                    
                    _loc_4 = _loc_2[_loc_3];
                    if (_loc_4.wave > this._maxWave)
                    {
                        this._maxWave = _loc_4.wave;
                    }
                    _loc_3++;
                }
            }
            return this._maxWave;
        }// end function

    }
}
